﻿Public Class HTML

	Shared Function Parse(Text As String) As body
		Dim body As New body
		body.Parse(Text)
		Return body
	End Function

	MustInherit Class Element

		Dim Children As New List(Of Element)
		Dim Parent As Element

		Sub Add(Element As Element)
			Children.Add(Element)
			Element.Parent = Me
		End Sub

		MustOverride Sub Parse(ByRef Text As String)

	End Class

	Class body : Inherits Element

		Public Overrides Sub Parse(ByRef Text As String)
			Do
				Dim Tag = Mid(Text.ClipUpto(">"), 2)
				Select Case Tag
					Case "<table>"
						Dim t As New table
						Add(t)
						t.Parse(Text)
					Case ""
						Return
					Case Else
						Warning("unknown tag: " & Tag)
				End Select
			Loop
		End Sub

	End Class

	Class table : Inherits Element

		Public Overrides Sub Parse(ByRef Text As String)
			Do
				Dim Tag = Mid(Text.ClipUpto(">"), 2)
				Select Case Tag
					Case "<tr>"
						Dim t As New tr
						Add(t)
						t.Parse(Text)
					Case "</table>"
						Return
					Case Else
						Warning("unknown tag: " & Tag)
				End Select
			Loop
		End Sub

	End Class

	Class tr : Inherits Element

		Public Overrides Sub Parse(ByRef Text As String)
			Do
				Dim Tag = Mid(Text.ClipUpto(">"), 2)
				Select Case Tag
					Case "<td>"
						Dim t As New td
						Add(t)
						t.Parse(Text)
					Case "</tr>"
						Return
					Case Else
						Warning("unknown tag: " & Tag)
				End Select
			Loop
		End Sub

	End Class

	Class td : Inherits Element

		Public content As String

		Public Overrides Sub Parse(ByRef Text As String)
			content = Text.ClipUpto("<")
			Dim Tag = Mid(Text.ClipUpto(">"), 2)
			Select Case Tag
				Case "<table>"
					Dim t As New table
					Add(t)
					t.Parse(Text)
				Case "</td>"
					Return
				Case Else
					Warning("unknown tag: " & Tag)
			End Select
		End Sub

	End Class

End Class

